import { Schema, model } from 'mongoose';

export interface IApplication {
  _id?: Schema.Types.ObjectId;
  branch: string; // 分支
  createdAt?: Date;
  git: string; // git url地址
  name: string; // 名称
  purePages?: any[]; // 页面，当type为pure-list时，有值
  remark: string; // 备注
  type: 'base' | 'pure-pages' | string; // 类型
  updatedAt?: Date;
}

export const ApplicationModel = model<IApplication>(
  'Application',
  new Schema<IApplication>(
    {
      branch: { required: true, type: String },
      git: { required: true, type: String },
      name: { required: true, type: String },
      purePages: {
        default: [],
        ref: 'PurePage',
        type: [Schema.Types.ObjectId],
      },
      remark: { type: String },
      type: { enum: ['base', 'pure-pages'], required: true, type: String },
    },
    {
      // 自动添加createdAt和updatedAt时间戳
      timestamps: true,
      toObject: {
        getters: true, // 包含虚拟字段
        minimize: false, // 包含空对象
        versionKey: false, // 不包含 __v 版本键
        virtuals: true, // 包含虚拟属性
      },
    },
  ),
);
